//
//  YCPraiseView.m
//  YCSchool
//
//  Created by Yanglixia on 16/5/15.
//  Copyright © 2016年 yanglinxia. All rights reserved.
//

#import "YCPraiseView.h"

@interface YCPraiseView()
@property (nonatomic,readonly) UIImageView *mainView;
@property (nonatomic,readonly) UIView *maskView;
@property (nonatomic,readonly) UIButton *praiseButton;
@property (nonatomic,readonly) UIButton *mercyButton;
@property (nonatomic,readonly) UIButton *commentsButton;
@property (nonatomic,copy) YCPraiseViewSendBlock send;
@end

@implementation YCPraiseView


@synthesize mainView = _mainView;
@synthesize praiseButton = _praiseButton;
@synthesize mercyButton = _mercyButton;
@synthesize commentsButton = _commentsButton;

- (instancetype)initWithFrame:(CGRect)frame
{
    frame = CGRectMake(0, 0, kScreenWidth, 45);
    if(self = [super initWithFrame:frame])
    {
        self.backgroundColor = [UIColor clearColor];
        [self layout];
    }
    return self;
}

- (void)layout
{
    CGFloat width = (kScreenWidth-150)/3;
    _mainView = [[UIImageView alloc] init];
    _mainView.userInteractionEnabled = YES;
    _mainView.backgroundColor = [UIColor clearColor];
    [_mainView setImage:[UIImage imageNamed:@"so_backBound"]];
    [self addSubview:_mainView];
    
    _praiseButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_praiseButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_praiseButton setImage:[UIImage imageNamed:@"so_praise-1"] forState:UIControlStateNormal];
    [_praiseButton setTitle:@" 赞" forState:UIControlStateNormal];
    _praiseButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [_praiseButton addTarget:self action:@selector(praiseButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
    [self.mainView addSubview:_praiseButton];
    
    _mercyButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_mercyButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_mercyButton setImage:[UIImage imageNamed:@"so_mercy"] forState:UIControlStateNormal];
    [_mercyButton setTitle:@" 痛" forState:UIControlStateNormal];
    _mercyButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [_mercyButton addTarget:self action:@selector(mercyButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
    [self.mainView addSubview:_mercyButton];
    
    _commentsButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_commentsButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [_commentsButton setImage:[UIImage imageNamed:@"so_reply"] forState:UIControlStateNormal];
    [_commentsButton setTitle:@" 回复" forState:UIControlStateNormal];
    _commentsButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [_commentsButton addTarget:self action:@selector(commentsButtonTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
    [self.mainView addSubview:_commentsButton];
    
    [self.mainView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.right.equalTo(self);
        make.height.equalTo(@45);
    }];
    
    [self.praiseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.and.left.equalTo(self.mainView);
        make.width.equalTo(@(width));
    }];
    
    [self.mercyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.mainView);
        make.left.equalTo(self.praiseButton.mas_right);
        make.width.equalTo(@(width));
    }];
    
    [self.commentsButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.mainView);
        make.left.equalTo(self.mercyButton.mas_right);
        make.width.equalTo(@(width));
    }];
}

- (void)showWithSendBlock:(YCPraiseViewSendBlock)block withView:(UIView *)view;
{
    UIView *rootView = [UIApplication sharedApplication].delegate.window.rootViewController.view;
    
    _maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    _maskView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    _maskView.backgroundColor = [UIColor clearColor];
    [rootView addSubview:_maskView];
    
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapFromGestureRecognizer:)];
    [_maskView addGestureRecognizer:tapGestureRecognizer];
    
    UISwipeGestureRecognizer *recognizer;
    
    recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [_maskView addGestureRecognizer:recognizer];
    
    recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
    [_maskView addGestureRecognizer:recognizer];
    
    recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
    [_maskView addGestureRecognizer:recognizer];
    
    recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
    [_maskView addGestureRecognizer:recognizer];
    
    self.send = block;
    [_maskView addSubview:self];
    
    [self mas_updateConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(view);
        make.height.equalTo(@35);
        make.width.equalTo(@(kScreenWidth-150));
        make.right.equalTo(view.mas_left).offset(-10);
    }];
}

- (void)didTapFromGestureRecognizer:(UITapGestureRecognizer *)recognizer
{
    [_maskView removeFromSuperview];
}

-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer
{
    [_maskView removeFromSuperview];
}

- (void)cancelPraiseView
{
    [_maskView removeFromSuperview];
}

///赞
- (void)praiseButtonTouchUpInside:(UIButton *)sender
{
    if(self.send) self.send(YCPraiseViewSelectItemTypePraise);
    [_maskView removeFromSuperview];
}

///怜
- (void)mercyButtonTouchUpInside:(UIButton *)sender
{
    if(self.send) self.send(YCPraiseViewSelectItemTypeMercy);
    [_maskView removeFromSuperview];
}

///回复
- (void)commentsButtonTouchUpInside:(UIButton *)sender
{
    if(self.send) self.send(YCPraiseViewSelectItemTypeComments);
    [_maskView removeFromSuperview];
}




@end
